<?php
class SinaController extends Controller {

	//微博获取数据初始化
	private function sinaini(){
		//获取token
		session_start();
		$sina_token_model = $this->model('sina_token');
		$tokendata = $sina_token_model->from('sina_token')->where('userid=?', $_SESSION['userid'])->select();
		
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/sina/');
		include_once( 'config.php' );
		include_once( 'saetv2.ex.class.php' );

		if(empty($tokendata)){
			$url = $this->create_url('sina/authorize');
			$this->show_message('用户还没对新浪微博授权，即将转入新浪微博授权页面',$url,1 );
		}

		$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $tokendata[0]['access_token'] );

		return $c;
	}

	//首页
	public function indexAction() {
		$c = $this->sinaini();
		$ms  = $c->home_timeline(); 
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		
		// print_r($ms);

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display();	
		
	}

	//首页
	public function atmeAction() {
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_id(); 
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		
		// print_r($ms);

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');	
		
	}

 	//授权
	public function authorizeAction(){
		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/sina/');
		include_once( 'config.php' );
		include_once( 'saetv2.ex.class.php' );
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
		
		$this->show_message('转入新浪微博授权页面',$code_url,1 );
	}

	//回调函数
	public function callbackAction(){
		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/sina/');
		include_once( 'config.php' );
		include_once( 'saetv2.ex.class.php' );
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$token = $o->getAccessToken( 'code', $keys ) ;
			} catch (OAuthException $e) {
			}
		}


		if ($token) {
			$_SESSION['token'] = $token;
			$sina_token_model = $this->model('sina_token');

			//看是否已经登录
			$sina_token_model = $this->model('sina_token');
			$sina_token_data = array();
			$sina_token_data['userid'] = $_SESSION['userid'] ;
			$sina_token_data['access_token'] = $_SESSION['token']['access_token'] ;

			$data = $sina_token_model->from('sina_token')->where('access_token=?', $sina_token_data['access_token'])->select();

			if(!empty($data)){
				// 已经授权，就直接进入咯
				$url = $this->create_url('sina/index');
				$this->show_message('授权成功，即将转入新浪微博',$url,1 );
			}else{
				// 没授权过，插入用户信息吧。
				if($sina_token_model->insert($sina_token_data)){
					$url = $this->create_url('sina/index');
					$this->show_message('授权成功，即将转入新浪微博',$url,1 );
				}else {
					$url = $this->create_url('sina/authorize');
					$this->show_message('授权失败，再次跳转授权',$url,1 );
				}
			}

		}
	}

	//音乐
	public function yinyueAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('全球经典音乐'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}

	//笑话
	public function xiaohuaAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('笑点很低有木有'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}

	//热点
	public function redianAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('平安南粤'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}

	//生活
	public function shenghuoAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('创意工坊'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}

	//创业
	public function chuangyeAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('创业基金会'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}

	//电影
	public function dianyingAction(){
		// user_timeline_by_id
		$c = $this->sinaini();
		$ms  = $c->user_timeline_by_name('北京电影学院'); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display('sina/index');

	}


	public function sentAction() {
		
	}

	public function ajaxAction() {
		$c = $this->sinaini();
		$ms  = $c->home_timeline(); 
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display();	
		
	}

	public function detailsAction(){
		$c = $this->sinaini();
		$mid = $this->get_params('mid');

		$ms  = $c->show_status($mid ); 
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		$coment  = $c->get_comments_by_sid("$mid" , 1, 5); 

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->assign('coment',$coment);
		$this->set_layout('main_no');
		$this->display();
	}

	public function wbdetailAction(){
		$c = $this->sinaini();
		$ms  = $c->home_timeline(); 
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息  
		

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display();	
	}

	public function addAction(){
		session_start();
		if(!empty($_POST['content'])){
			$content = $this->get_params('content');
			$sina_content_model = $this->model('sina_content');
			$userdata = array();
			$userdata['content'] = $content;
			$userdata['time'] = time();
			$userdata['ip'] = "127.0.0.1";
			$userdata['uid'] = $_SESSION['userid'];
			$sina_content_model->insert($userdata);
			$c = $this->sinaini();
			$ms  = $c->update($content);
			$url = $this->create_url('sina/index');
			$this->show_message('发送成功，返回首页',$url,1 );
		}else{
			$this->display();
		}
		
	}

	public function replyAction(){
		// reply( $sid, $text, $cid, $without_mention = 0, $comment_ori = 0 )
		$content = $this->get_params('pinglun');
		$id = $this->get_params('mid');
		$c = $this->sinaini();
		$comment = $this->get_params('pinglun');
		$ms  = $c->send_comment( $id , $comment , $comment_ori = 0);

		$url = $this->create_url('sina/index');
		$this->show_message('评论成功，返回首页',$url,1 );

	}

	

	public function testAction(){
		$c = $this->sinaini();
		// $ms  = $c->get_comments_by_sid("3442720989452215"); 
		$ms  = $c->show_status(3442720989452215 ); 
		print_r($ms);
	}
}